package org.android.application.marksixmanage.grab;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.android.application.marksixmanage.model.LotteryPlatform;
import org.android.application.marksixmanage.model.LotteryResult;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public abstract class GrabAbstract {
	
	private static Map<Integer, GrabAbstract> grabMap;
	
	static{
		grabMap = new HashMap<Integer, GrabAbstract>();
		grabMap.put(LotteryPlatform.Lottery_SiX.getPlatformId(), new LotterySixGrab());
	}
	
	public static GrabAbstract getInstance(int platformId){
		return grabMap.get(platformId);
	}
	
	public abstract LotteryResult grabData(String url,String encode,String issueNumber);
	
	public abstract LotteryResult parseString(String html,String encode,String issueNumber);
	
	protected String getHtml(String url,String encode){
		
		DefaultHttpClient httpClient = new DefaultHttpClient();
		
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = httpClient.execute(httpGet);
			InputStream is = response.getEntity().getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, encode));
			StringBuffer sb = new StringBuffer();
			String str = null;
			while (true) {
				str = br.readLine();
				if (str == null) {
					break;
				}
				sb.append(str);
			}

			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			Log.d("grab", "grab webSite error" + e.getMessage());
		}
		return null;
		
	}
}
